//
//  JDDownLoadQRCode.swift
//  DragonWallet
//
//  Created by volantPig on 2024/4/22.
//

import UIKit
import SwiftyJSON

enum JDQRCodeType {
    /// 车辆下载吗
    case cart
    /// 用户下载码
    case user
}

class JDDownLoadQRCode: UIView {

    var QRCodeBgView: UIImageView?
    var closeButton: UIButton?
    var QRCodeImgV: UIImageView?
    var QRCodeType: JDQRCodeType = .cart
    
    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, screenWidth, screenHeight))
        self.setupConfig()
        self.setupUIElements()
        self.getNetData()
    }
    
    func setupConfig() {
        self.backgroundColor = color("#000000").withAlphaComponent(0.4)
    }
    
    func setupUIElements() {
    
        self.QRCodeBgView = UIImageView()
        self.addSubview(self.QRCodeBgView!)
        self.QRCodeBgView?.snp.makeConstraints({ make in
//            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-62)
//            make.size.equalTo(CGSize(width: 328, height: 445))
            make.leading.equalTo(self).offset(21)
            make.trailing.equalTo(self).offset(-21)
        })
        self.QRCodeBgView?.contentMode = .scaleAspectFit
        
        if self.QRCodeType == .cart {
            /// 车辆下载码
            self.QRCodeBgView?.image = UIImage(named: "QRCode_user")?.withRenderingMode(.alwaysOriginal)
        } else {
            /// 用户下载码
            self.QRCodeBgView?.image = UIImage(named: "QRCode_user")?.withRenderingMode(.alwaysOriginal)
        }
        
        self.QRCodeImgV = UIImageView()
        self.QRCodeBgView?.addSubview(self.QRCodeImgV!)
        self.QRCodeImgV?.snp.makeConstraints({ make in
            make.left.equalTo(self).offset(76)
            make.right.equalTo(self).offset(-76)
            make.centerY.equalTo(self.QRCodeBgView!).offset(-45)
            make.height.equalTo(self.QRCodeImgV!.snp.width)
        })
        
        self.closeButton = UIButton(type: .custom)
        self.addSubview(self.closeButton!)
        self.closeButton?.snp.makeConstraints({ make in
            make.centerX.equalTo(self)
            make.top.equalTo(self.QRCodeBgView!.snp.bottom)//.offset(44)
            make.size.equalTo(CGSize(width: 42, height: 42))
        })
        self.closeButton?.setImage(UIImage(named: "QRCodeClose")?.withRenderingMode(.alwaysOriginal), for: .normal)
        self.closeButton?.addTarget(self, action: #selector(closeButtonAction) , for: .touchUpInside)
    }
    
    
    
    
    
    
    
    @objc func closeButtonAction() {
        self.removeFromSuperview()
        printLog("关闭页面")
    }
    
    func getNetData() {
        JDNetWorkRequest(.fetchUserRecommendCode) { responseStr in
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                let dic = JSON(commonModel?.data as Any).dictionaryValue
                let shareLink = JSON(dic["shareLink"] as Any).stringValue
                Task {
                    QRCodeTool.setQRCodeToImageView(self.QRCodeImgV!, shareLink)
                }
            } else {
                self.toast(message: "网络异常")
            }
            
        } failure: { error in
            self.toast(message: error.errorDescription ?? "")
        } error: { errorCode in
            self.toast(message: "网络异常")
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
